using namespace std;

#include<stdio.h>
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdlib>
#include<random_access.hpp>

int floatlen,rows, cols, row1start,row1end,row2start,row2end;
random_access_t * random_access;

void compute(int row1start, int row1end, int row2start, int row2end){
  float vec1[cols];
  float vec2[cols];
  for(int row1 = row1start;row1<=row1end;++row1){
    random_access->extract_vec(row1,cols,vec1);
    for(int row2 = row2start;row2<=row2end;++row2){
      random_access->extract_vec(row2,cols,vec2);
      double val = 0;
      for(int col=0;col<cols;++col){
        val+=vec1[col]*vec2[col];
        //cerr<<col<<":"<<vec1[col]<<","<<vec2[col]<<" = "<<val<<endl;
      }
      cout<<row1<<"\t"<<row2<<"\t"<<val<<endl;
    }
  }
}

int main(int argc,char * argv[]){
  if(argc<8) {
    cerr<<"Usage: <filename> <input rows> <input cols> <row1start> <row1end> <row2start> <row2end>; You entered "<<argc<<" arguments of:";
    for(int i=0;i<argc;++i){
      cerr<<" "<<argv[i];
    }
    cerr<<endl;
    return 1;
  }
  int arg=0;
  floatlen = sizeof(float);
  const char * filename=argv[++arg];
  rows = atoi(argv[++arg]);
  cols = atoi(argv[++arg]);
  row1start = atoi(argv[++arg]);
  row1end = atoi(argv[++arg]);
  row2start = atoi(argv[++arg]);
  row2end = atoi(argv[++arg]);
  random_access = new random_access_t(filename,rows,cols);
  compute(row1start,row1end,row2start,row2end);
  delete random_access;
  return 0;
}
